
import numpy as np
import pandas as pd
# import pandas_profiling as pp
import matplotlib.pyplot as plt
import seaborn as sns



data = pd.read_csv(r"C:\Users\Administrator\Downloads\阶段三\阶段三\Dataset.csv")

# 取出所有有空值的记录  （5分）
# 用每列的均值填充其缺失值
# 删除date为空的行
cond = [i==True for i in data['Date'].isna()]
[data.drop(index=i,inplace=True) for i in data[cond].index]

# print(data.isna().sum())
# print("***")
# 填充缺失值的列
cc = [i for i in data.isna().sum()>0]
c = data.columns[cc] #缺失值的列
def get_mean(col):
    res = round(data[col].mean(),2)
    return res
for i in c:
    data[i].fillna(get_mean(i),inplace=True)
# print(data.isna().sum())
# data["Date"] = [i.replace("/","") for i in data["Date"]]# 6/30/2013
# print(data["Date"])
# y = data["Date"]
data.drop(columns="Date",inplace=True)
# col = data.columns.values.tolist()
# col.remove('Date')
# x = data[col].copy()
# # print(x)
# data = data.set_index(data["Date"])



from sklearn.preprocessing import StandardScaler

#标准化处理连续型特征 （10分）
# print(date2)
std = StandardScaler().fit_transform(data)# print(std)
# 划分数据集（5分）

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

y = data.pop("Present_Tmax")
Xtrain,Xtest,Ytrain,Ytest = train_test_split(data,y,test_size=0.3,random_state=420)
#线性回归模型
lr = LinearRegression()
#训练数据
lr.fit(Xtrain,Ytrain)
print(lr.score(Xtrain,Ytrain)) #0.648

#计算MSE与RMSE
from sklearn import metrics
y_pred =lr.predict(Xtrain)
y_test_pred = lr.predict(Xtest)
MSE = metrics.mean_squared_error(Ytest, y_pred)
RMSE = np.sqrt(metrics.mean_squared_error(Ytest, y_pred))
# print(MSE)
# print(RMSE)

#绘制线性拟合图
plt.scatter(range(len(Ytest)),sorted(Ytest),s=2)
plt.show()
# 使用散点图观测
plt.scatter(
    data, # 横坐标
    data["Present_Tmax"], # 纵坐标
    alpha = 0.5) # 透明度
# 残差图

# 热力图
plt.imshow(data,
           cmap=plt.cm.RdBu_r,# 颜色
           )